/********************************************************************************
  * @file     SD93F115B_lcd.c   						    	
  * @author   TEST TEAM 												    	
  * @version  v0															    	
  * @date     Septemper 2022												    	
  * @brief 
  *           
  * Copyright (C) 2022  Hangzhou SDIC Microelectronics Co., Ltd  
*********************************************************************************/

/* Includes */

#include "SD93F115B_lcd.h"
#include "SD93F115B_rcc.h"
   
/*----------------------------------------
 * 函数功能：LCD相关寄存器重设为上电默认值
 * 入口参数：None
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *---------------------------------------*/ 
 void LCD_DeInit(void)
 {
	 
	RCC_APBPeriphResetCmd(RCC_APBPeriph_LCD, ENABLE);
    RCC_APBPeriphResetCmd(RCC_APBPeriph_LCD, DISABLE);

 }

/*------------------------------------------------
 * 函数功能：初始化LCD相关参数
 * 入口参数：LCD_InitStruct
 *           LCD_InitStruct: pointer to a LCD_InitStruct structure that
 *           contains the configuration information for the specified LCD peripheral.
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *------------------------------------------------*/
void LCD_Init( LCD_InitTypeDef* LCD_InitStruct)
{
    /* Check the parameters */
	assert_param(IS_LCD_WAVE(LCD_InitStruct->LCDWAVE));
	assert_param(IS_LCD_DUTY(LCD_InitStruct->LCDDUTY));
	assert_param(IS_LCDCLKS_DIV(LCD_InitStruct->LCDCLKS_DIV));
	assert_param(IS_LCD_CLKS(LCD_InitStruct->LCDCLKS));
	assert_param(IS_LCD_BIAS(LCD_InitStruct->BIAS));
	assert_param(IS_LCD_LCDBL(LCD_InitStruct->LCDBL));
	
	uint32_t tmpreg=0,tmpreg1=0;

	LCD->COMR = LCD_InitStruct->LCDPin_Mode_COM;
	LCD->SEGR1 = LCD_InitStruct->LCDPin_Mode_SEG1;
	LCD->SEGR2 = LCD_InitStruct->LCDPin_Mode_SEG2;

	if (LCD_InitStruct->BIAS != DISABLE)
	{
		LCD->CR |= LCD_CR_BIAS ;
	}
	else
	{
		LCD->CR &= ~LCD_CR_BIAS;	    
	}
	
	if (LCD_InitStruct->LCDBL != DISABLE)
	{
		LCD->CR |= LCD_CR_LCDBL ;
	}
	else
	{
		LCD->CR &= ~LCD_CR_LCDBL;	    
	}

	LCD_CLKConfig(LCD_InitStruct->LCDCLKS,LCD_InitStruct->LCDCLKS_DIV);
	
	tmpreg=0xFFFEFCFF&LCD->CR;
	tmpreg1=LCD_InitStruct->LCDDUTY;
	tmpreg |= tmpreg1<<8;
	
	tmpreg1=LCD_InitStruct->LCDWAVE;
	tmpreg |= tmpreg1<<16;
	
	LCD->CR=tmpreg;
	

}
/*-----------------------------------------------
 * 函数功能：使能LCD功能
 * 入口参数：ENABLE或者DISABLE
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *-----------------------------------------------*/
void LCD_Cmd(FunctionalState NewState)
{
	/* Check the parameters */
	assert_param(IS_FUNCTIONAL_STATE(NewState));
	if (NewState != DISABLE)
	{
		LCD->CR |= LCD_CR_LCDEN ;
	}
	else
	{
		LCD->CR &= ~LCD_CR_LCDEN;	    
	}
}

/*----------------------------------------
 * 函数功能：配置CPR的工作时钟
 * 入口参数：CPRCK: LCD_IHRC_DIV8,LCD_IHRC_DIV16,LCD_IHRC_DIV32,LCD_ILRC_MUL2,LCD_ILRC,LCD_XTOSC1,LCD_XTOSC1_DIV2
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *---------------------------------------*/  
void LCD_CPRCLKConfig(uint8_t CPRCK)
{
	/* Check the parameters */
	assert_param(IS_LCD_CPRCLK(CPRCLK));
	
	uint8_t tmpreg=0;
	tmpreg=0x1f&LCD->CPMR;
	LCD->CPMR=(tmpreg|CPRCK);	
}
/*----------------------------------------
 * 函数功能：CPR模块使能
 * 入口参数：ENABLE或者DISABLE
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *---------------------------------------*/ 
void LCD_CPRCmd(FunctionalState NewState)
{
	/* Check the parameters */
	assert_param(IS_FUNCTIONAL_STATE(NewState));
	
	if(NewState==DISABLE)
		LCD->CPMR&=~LCD_CPMR_CPREN;
	else
		LCD->CPMR|=LCD_CPMR_CPREN;
}

/*----------------------------------------
 * 函数功能：配置VLCD的输出电平值
 * 入口参数：VLCDLevel
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *---------------------------------------*/ 
void LCD_VLCDLevelConfig(uint8_t VLCDLevel)
{
	/* Check the parameters */
	assert_param(IS_LCD_VLCD_Level(VLCDLevel));
	
	uint8_t tmpreg=0;
	tmpreg=0xe1&LCD->CPMR;
	LCD->CPMR=(tmpreg|VLCDLevel);
	
}

/*----------------------------------------
 * 函数功能：配置LCD的工作时钟
 * 入口参数：LCDCLKS,LCDCLKS_DIV
 * 			 LCDCLKS: LCDCLKS_ILRC_DIV2,LCDCLKS_ILRC,LCDCLKS_XTOSC1_DIV2,LCDCLKS_XTOSC1
 *           LCDCLKS_DIV: LCDCLKS_DIV16,LCDCLKS_DIV32,LCDCLKS_DIV64,LCDCLKS_DIV128
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *---------------------------------------*/
void LCD_CLKConfig(uint8_t LCDCLKS,uint8_t LCDCLKS_DIV)
{
   /* Check the parameters */	
	assert_param(IS_LCD_CLKS(LCD_InitStruct->LCDCLKS));
	assert_param(IS_LCDCLKS_DIV(LCD_InitStruct->LCDCLKS_DIV));
	uint16_t tmpreg=0;
	
	tmpreg=0xFF93 & LCD->CR;
	LCD->CR = tmpreg|(((uint16_t)LCDCLKS<<5)|((uint16_t)LCDCLKS_DIV<<2));

}

/*------------------------------------------------
 * 函数功能：对LCD单个数据显示寄存器进行配置
 * 入口参数：LCD_DRx,Data
 *           LCD_DRx:  where x can be (0 ..10) to select the LCD peripheral.
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *------------------------------------------------*/ 
void LCD_DRConfig(uint8_t LCD_DRx,uint32_t Data)
{
	/* Check the parameters */
	assert_param(IS_LCD_DR(LCD_DRx));
	
	switch(LCD_DRx)
	{
		case LCD_DR0:
			LCD->DR0=Data;
			break;
			
		case LCD_DR1:  
			LCD->DR1=Data;
			break;
			
		case LCD_DR2: 
			LCD->DR2=Data;
			break;
		case LCD_DR3:  
			LCD->DR3=Data;
			break;
			
		case LCD_DR4: 
			LCD->DR4=Data;
			break;
			
		case LCD_DR5:  
			LCD->DR5=Data;
			break;
			
		case LCD_DR6: 
			LCD->DR6=Data;
			break;
			
		case LCD_DR7:
			LCD->DR7=Data;
			break;
			
		case LCD_DR8:  
			LCD->DR8=Data;
			break;
			
		case LCD_DR9: 
			LCD->DR9=Data;
			break;
			
		case LCD_DR10: 
			LCD->DR10=Data;
			break;	
		default:
			break;		
	}
}

/*-----------------------------------------------
 * 函数功能：整体配置LCD数据寄存器
 * 入口参数：LCD_DRStruct: pointer to a LCD_InitStruct structure that
 *           contains the configuration information for the specified LCD peripheral.
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *-----------------------------------------------*/ 
void LCD_StructDR(LCD_DRTypeDef* LCD_DRStruct)
{
  LCD->DR0 = LCD_DRStruct->LCD_Data0;
  LCD->DR1 = LCD_DRStruct->LCD_Data1;
  LCD->DR2 = LCD_DRStruct->LCD_Data2;
  LCD->DR3 = LCD_DRStruct->LCD_Data3;
  LCD->DR4 = LCD_DRStruct->LCD_Data4;
  LCD->DR5 = LCD_DRStruct->LCD_Data5;
  LCD->DR6 = LCD_DRStruct->LCD_Data6;
  LCD->DR7 = LCD_DRStruct->LCD_Data7;
  LCD->DR8 = LCD_DRStruct->LCD_Data8;
  LCD->DR9 = LCD_DRStruct->LCD_Data9; 
  LCD->DR10 = LCD_DRStruct->LCD_Data10; 

}

/*----------------------------------------------------------
 * 函数功能：整体初始化LCD相关参数，包括CPR,VLCD电平的设置
 * 入口参数：None
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *---------------------------------------------------------*/ 
void LCD_StructInit(void)
 {
	LCD_InitTypeDef LCD_InitStruct;
	LCD_InitStruct.BIAS = DISABLE;	//20211108  1/3 change to 1/4
	LCD_InitStruct.LCDBL = ENABLE;
	LCD_InitStruct.LCDDUTY = LCDDUTY_6COM;	//20210713
	LCD_InitStruct.LCDCLKS = LCDCLKS_ILRC_DIV2;//
	LCD_InitStruct.LCDCLKS_DIV = LCDCLKS_DIV4;		//20210723 64 to 32
	LCD_InitStruct.LCDWAVE = LCDWAVE_B;

	LCD_InitStruct.LCDPin_Mode_COM=0xff;			//6com && seg 2 3
	LCD_InitStruct.LCDPin_Mode_SEG1=0x8fffa000;		//20211125	seg 15 16 17 18 19 20 21 22 23 24 25    27 31
	LCD_InitStruct.LCDPin_Mode_SEG2=0x3F8;			//20211125 	seg 35 36 37 38 39 40   41
	LCD_Init(&LCD_InitStruct);
	LCD_CPRCmd(ENABLE);
	LCD_CPRCLKConfig(LCD_IHRC_DIV8);//
	LCD_VLCDLevelConfig(LCD_VLCDLevel_3V6);		//20211108 3v6 change to 4v2
	//LCD_CPRCmd(ENABLE);
	LCD_Cmd(ENABLE);
	LCD_ClearDisALL();
	
 }
 
 /*---------------------------------------------
 * 函数功能：LCD全显示
 * 入口参数：None
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *---------------------------------------------*/ 
void LCD_DisALL(void)
 {
	LCD_DRTypeDef LCD_DRStruct;
	LCD_DRStruct.LCD_Data0=0xffffffff;
	LCD_DRStruct.LCD_Data1=0xffffffff;
	LCD_DRStruct.LCD_Data2=0xffffffff;
	LCD_DRStruct.LCD_Data3=0xffffffff;
	LCD_DRStruct.LCD_Data4=0xffffffff;
	LCD_DRStruct.LCD_Data5=0xffffffff;
	LCD_DRStruct.LCD_Data6=0xffffffff;
	LCD_DRStruct.LCD_Data7=0xffffffff;
	LCD_DRStruct.LCD_Data8=0xffffffff;
	LCD_DRStruct.LCD_Data9=0xffffffff;
	LCD_DRStruct.LCD_Data10=0xffffffff;			
	LCD_StructDR(&LCD_DRStruct);
 
 }
 
 /*----------------------------------------------
 * 函数功能：清LCD显存
 * 入口参数：None
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *----------------------------------------------*/ 
void LCD_ClearDisALL(void)
 {
	 
	LCD_DRTypeDef LCD_DRStruct;     //数显清0
	LCD_DRStruct.LCD_Data0=0;
	LCD_DRStruct.LCD_Data1=0;
	LCD_DRStruct.LCD_Data2=0;	
	LCD_DRStruct.LCD_Data3=0;
	LCD_DRStruct.LCD_Data4=0;
	LCD_DRStruct.LCD_Data5=0;
	LCD_DRStruct.LCD_Data6=0;
	LCD_DRStruct.LCD_Data7=0;
	LCD_DRStruct.LCD_Data8=0;
	LCD_DRStruct.LCD_Data9=0;
	LCD_DRStruct.LCD_Data10=0;			
	LCD_StructDR(&LCD_DRStruct); 
 }
 
 /****************************END OF FILE****************************/








